package cn.wen233.iocdemo.application.service.impl;

import cn.wen233.iocdemo.application.service.Esp8266Service;
import cn.wen233.iocdemo.domain.enums.LedState;
import cn.wen233.iocdemo.domain.enums.MotorState;
import cn.wen233.iocdemo.domain.model.DHT11;
import cn.wen233.iocdemo.infrustructure.util.TcpChannel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author wenei
 * @date 2021-06-22 9:24
 */
//@Service
public class TcpEsp8266ServiceImpl implements Esp8266Service {

    private static final Logger log = LoggerFactory.getLogger(TcpEsp8266ServiceImpl.class);

    private static final String ip = "192.168.43.215";

    private static final int port = 8090;

    private LedState curLedState = LedState.OFF;

    private MotorState curMotorState = MotorState.OFF;

    @Override
    public DHT11 dht11() {
        return null;
    }

    @Override
    public String ledControl(boolean status) {
        String command = curLedState.getCommand();
        String message = curLedState.getMessage();
        curLedState = curLedState == LedState.OFF ? LedState.ON : LedState.OFF;
        TcpChannel.send(ip, port, command);
        log.info("主机向 {}:{} 发送 {} {}", ip, port, command, message);
        return message;
    }

    @Override
    public String motorControl(boolean status) {
        String command = curMotorState.getCommand();
        String message = curMotorState.getMessage();
        curMotorState = curMotorState == MotorState.OFF ? MotorState.ON : MotorState.OFF;
        TcpChannel.send(ip, port, command);
        log.info("主机向 {}:{} 发送 {} {}", ip, port, command, message);
        return message;
    }
}
